<?php
// 本类由系统自动生成，仅供测试用途
class IndexAction extends Action {
    public function index(){
    	//$all=D('Post')->select();
    	$pd=$_GET['pd'];
    	$bkk=array("zh","mh","yx","dm","gz");
    	if(!$pd){
    		$pd="zh";
    	}elseif(!in_array($pd,$bkk)){
    		$pd="zh";

    	}
    	$all=D('Post')->where("pd='".$pd."'")->order("time desc")->select();
    	$count=count($all);
    	import('ORG.Util.Page');
    	$Page=new Page($count,25);
    	if($_GET['p']<1){
				$_GET['p']=1;
			}else{
				$_GET['p']=(int)$_GET['p'];//
			}
    	$Page->setConfig("header","篇文章");
    	$list=array_slice($all,($_GET['p']-1)*25,25);
    	$show=$Page->show();
    	//var_dump($all);
		$this->assign("post",$list);
		$this->assign("page",$show);
		$this->display();
	}
	public function post(){
		$pid=(int)$_GET['pid'];
		$post=D('Post')->find($pid);
		D('Post')->where("pid='".$pid."'")->setInc("view");
		$repost=D('Repost')->re($pid);
		$count=count($repost);
		import('ORG.Util.Page');
		$Page=new Page($count,25);
		$Page->setConfig('header',"条评论");
		if($_GET['p']<1){
				$_GET['p']=1;
			}else{
				$_GET['p']=(int)$_GET['p'];//
			}
		$list=array_slice($repost, 25*($_GET['p']-1),25);
		$this->assign('repost',$list);
		$show=$Page->show();
		$this->assign('page',$show);

		//$this->assign('repost',$repost);
		$this->assign('post',$post);
		$this->display();

	}
	function re(){		
			$x=D('Repost')->repost($_POST);
			//$x返回发布的消息
			$all=D('Repost')->rere($x['rid']);
			foreach ($all as $key => $value) {
				$user=D('User')->find($value['uid']);
				$x[$key]['uid']=$user['name'];
				$x[$key]['time']=date("Y-m-d H:i",$all[$key]['time']);
				$x[$key]['text']=$all[$key]['text'];
				$x[$key]['num']=$all[$key]['num'];
				# code...
			}
			$this->ajaxReturn($x);
		}
	function user(){
		$uid=(int)$_GET['uid'];
		if($user=D('User')->find($uid)){
			$post=D('Post')->where("uid='".$uid."'")->order("time desc")->select();
			$count=count($post);
			import('ORG.Util.Page');
			$Page=new Page($count,10);
			if($_GET['p']<1){
				$_GET['p']=1;
			}else{
				$_GET['p']=(int)$_GET['p'];//
			}
			$list=array_slice($post, 10*($_GET['p']-1),10);
			$this->assign("post",$list);
			$show=$Page->show();
			$this->assign("page",$show);
			$this->assign("user",$user);
			$this->display();
		}
	}
	function search(){
		$_POST['search'];
		if(!$_SESSION['uid']){
			$this->error("搜索技能只有登入后才能释放！","/index.php/User/login");
			return;
		}
		$search=json_encode($_GET['search']);
		$search=trim($search,"\"");
		$search=addslashes($search);
		$where['tag']=array('like',"%".$search."%");
		$where['title']=array('like',"%".$_GET['search']."%");
		$where['_logic']='or';
		if($_GET['type']=="all" ||!$_GET['type']){
		}elseif ($_GET['type']=="zh") {
			$se['pd']="zh";
			$se['_logic']='AND';
		}elseif ($_GET['type']=="gz") {
			$se['pd']="gz";
			$se['_logic']='AND';
		}elseif ($_GET['type']=="dm") {
			$se['pd']="dm";
			$se['_logic']='AND';
		}elseif ($_GET['type']=="mh") {
			$se['pd']="mh";
			$se['_logic']='AND';
		}elseif ($_GET['type']=="yx") {
			$se['pd']="yx";
			$se['_logic']='AND';
		}else{

		}
		$se['_complex']=$where;
		$all=D('Post')->where($se)->select();
		$count=count($all);
		import('ORG.Util.Page');
		$Page=new Page($count,10);
			if($_GET['p']<1){
				$_GET['p']=1;
			}else{
				$_GET['p']=(int)$_GET['p'];//
			}
		$list=array_slice($all, 10*($_GET['p']-1),10);
		$this->assign("post",$list);
		$show=$Page->show();
		$this->assign("page",$show);
		$this->display();


		//echo trim($search,"\"");
	}

}


